package com.itheima.controller;

import com.itheima.pojo.Assignment;
import com.itheima.pojo.User;
import com.itheima.service.AssignmentService;
import com.itheima.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/v1/work")
public class WorkController {

    @Autowired
    private AssignmentService assignmentService;


    @PostMapping("/publishWork")
    public ResponseResult publishWork(@RequestBody Assignment assignment, HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");

        assignment.setUserId(user.getId());

        return assignmentService.addAssignment(assignment);
    }

    @PostMapping("/showWorks")
    public ResponseResult showWorks(@RequestBody Map<String,String> map) throws ParseException {
//        2022-11-13T14:22:20.505Z
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        return assignmentService.findStudentAssignmentByTimeAndType(simpleDateFormat.parse(map.get("time")),Long.parseLong(map.get("type")));
    }

   @PostMapping("/findNotWorkByTime")
    public ResponseResult findNotWorkByTime(@RequestBody Map<String,String> map) throws ParseException {
       return assignmentService.findStudentByClassIdAndTypeAndTime(map);
   }
}
